package org.mysocial.services.business.impl;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.mysocial.datamodel.Account;
import org.mysocial.datamodel.Comment;
import org.mysocial.datamodel.Like;
import org.mysocial.datamodel.Link;
import org.mysocial.datamodel.Picture;
import org.mysocial.datamodel.Post;
import org.mysocial.datamodel.Text;
import org.mysocial.datamodel.Upload;
import org.mysocial.services.business.NotificationBusiness;
import org.mysocial.services.business.PostBusiness;
import org.mysocial.services.dao.PostDAO;
import org.mysocial.services.dao.ReplyDAO;
import org.mysocial.util.FormatHelper;

/**
 * Session Bean implementation class PostBusiness
 */
@Stateless
public class PostBusinessImpl implements PostBusiness
{
	@EJB
	private PostDAO postDAO;

	@EJB
	private ReplyDAO replyDAO;

	@EJB
	private NotificationBusiness notificationBusiness;

	@Override
	public <T extends Post> Post make(Class<T> postNature)
	{
		try
		{
			final Post post = postNature.newInstance();

			return post;
		} catch (final Exception e)
		{
			throw new RuntimeException(e.getMessage());
		}
	}

	@Override
	public Upload postPicture(Account account, String text, Picture picture)
	{
		final Upload uploadPost = new Upload();
		uploadPost.setText(text);
		uploadPost.setAccount(account);
		uploadPost.setBinaryContent(picture);
		uploadPost.setTime(new Date());

		return (Upload) postDAO.persist(uploadPost);
	}

	@Override
	public Text postText(Account account, String text)
	{
		final Text textPost = new Text();
		textPost.setText(text);
		textPost.setAccount(account);
		textPost.setTime(new Date());

		return (Text) postDAO.persist(textPost);
	}

	@Override
	public Link postUrl(Account account, String text, String url)
	{
		if (!FormatHelper.isUrlWellformed(url))
		{
			throw new RuntimeException("url : " + url + " is not valid");
		}

		Link link = new Link();
		link.setText(text);
		link.setAccount(account);
		link.setUrl(url);
		link.setTime(new Date());

		link = (Link) postDAO.persist(link);

		return link;
	}

	@Override
	public void removeComment(Comment comment)
	{
		replyDAO.remove(comment);
	}

	@Override
	public void removeLike(Like like)
	{
		replyDAO.remove(like);
	}

	@Override
	public void removePost(Post post)
	{
		postDAO.remove(post);
	}

	@Override
	public Comment replyComment(Account account, String text, Post target)
	{
		Comment comment = new Comment();
		comment.setText(text);
		comment.setAccount(account);
		comment.setPost(target);
		comment.setTime(new Date());

		comment = (Comment) replyDAO.persist(comment);

		// TODO rendere async EJB31
		notificationBusiness.notifyComment(null, comment);

		return comment;
	}

	@Override
	public Like replyLike(Account account, Post target)
	{
		Like like = new Like();
		like.setAccount(account);
		like.setPost(target);
		like.setTime(new Date());

		like = (Like) replyDAO.persist(like);

		notificationBusiness.notifyLike(target.getAccount(), like);

		return like;
	}
}
